<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./static/static/vue.min.js"></script>
    <script src="./static/static/axios.min.js"></script>
    <!-- 本地引入lib-master -->
    <link rel="stylesheet" type="text/css" href="./static/lib-master/theme-chalk/index.css" />
    <script src="./static/lib-master/index.js" type="text/javascript" charset="utf-8"></script>
</head>

<body>

    <div id='app'>
        <h1>登陆页面</h1>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="手机号" prop="userId">
                <el-input type="text" v-model="ruleForm.userId" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>

    <script>
        new Vue({
            el: '#app',
            data() {
                var validateuserId = (rule, value, callback) => {
                    if (value === '') {
                        callback(new Error('请输入用户名'));
                    } else {
                        if (this.ruleForm.password !== '') {
                            this.$refs.ruleForm.validateField('password');
                        }
                        callback();
                    }
                };
                var validateuserId2 = (rule, value, callback) => {
                    if (value === '') {
                        callback(new Error('请输入密码'));
                    } else {
                        callback();
                    }
                };
                return {
                    baseurl: "http://localhost:88/api",
                    ruleForm: {
                        userId: '',
                        password: ''
                    },
                    rules: {
                        userId: [
                            { validator: validateuserId, trigger: 'blur' }
                        ],
                        password: [
                            { validator: validateuserId2, trigger: 'blur' }
                        ]
                    }
                };
            },
            //方法集合
            methods: {
                submitForm(formName) {
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            axios.post(`${this.baseurl}/user/user/login`, this.ruleForm)
                                .then(({ data }) => {
                                    if (data && data.code === 0) {
                                        this.$message({
                                            message: '操作成功',
                                            type: 'success',
                                            duration: 1500,
                                            onClose: () => {
                                                this.visible = false
                                                this.$emit('refreshDataList')
                                            }
                                        })
                                        console.log(data.userId);
                                        localStorage.setItem("userId", data.userId)
                                        localStorage.setItem("token", data.token)
                                        location = "./actList.html"
                                        console.log(localStorage.getItem("token"))

                                    } else {
                                        this.$message.error(data.msg)
                                    }
                                })

                        } else {
                            console.log('error submit!!');
                            return false;
                        }
                    });
                },
                resetForm(formName) {
                    this.$refs[formName].resetFields();
                }


            },
            created() {

            },
        })
    </script>

</body>

</html>